﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq.Dynamic;
using IronOne.SalesOne.BusinessObjects;
using System.Transactions;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqProduct
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);     
        public List<Product> GetProducts(Product product, int page)
        {
            StringBuilder qry = new StringBuilder();


            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                if (product.ProductCategoryCode != 0)
                {
                    qry.Append("ProductCategoryCode=" + product.ProductCategoryCode);
                    
                    if (product.PlanCode != "" && product.PlanName != "")
                    {
                        qry = qry.Append(" AND " + " PlanCode =\"" + product.PlanCode + "\"" + " AND " + " PlanName.Contains(\"" + product.PlanName + "\")");                     
                    }
                    else if (product.PlanCode != "" && product.PlanName == "")
                    {
                        qry = qry.Append(" AND " + " PlanCode =\"" + product.PlanCode + "\"");
                        ////qry += " AND " + " PlanCode =\""+product.PlanCode+"\"";
                    }
                    else if (product.PlanName != "" && product.PlanCode == "")
                    {
                        qry = qry.Append(" AND " + " PlanName.Contains(\"" + product.PlanName + "\")");
                        ////qry += " AND " + " PlanName =\""+product.PlanName+"\""; 
                        //qry = qry.Append(" AND " + " PlanName =\"" + product.PlanName + "\"");
                    }

                }


                IQueryable<ProductEntity> query = db.ProductEntities;

                //query = db.ProductEntities.Where(qry);
                query = db.ProductEntities.Where(qry.ToString());
                var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
                return pageData.Select(c => Mapper.ToProductObject(c)).ToList();
               
            }
        }


        public List<Product> GetProduct(string planCode)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                var query = from product in db.ProductEntities
                            where product.PlanCode == planCode
                            select product;

                return query.Select(c => Mapper.ToProductObject(c)).ToList();

            }
        }

        public List<Product> GetProductsss(int product)
        {

            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                IQueryable<ProductEntity> query = db.ProductEntities;

              
                var query1 = from prod in db.ProductEntities
                             where prod.ProductCategoryCode == product
                             select prod;

                //var pageData = query1.Skip((page - 1) * pageSize).Take(pageSize);
                return query1.Select(c => Mapper.ToProductObject(c)).ToList();
               
            }
        }


        public int  UpdateProductTrans(Product product)
        {

            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                UpdateProduct(product,db);
                UpdatePlan(product,db);
                //UpdateProductCategory(product);

                //scope.Complete();
                db.SubmitChanges();
                return 1;
            }

        }


        public int UpdateProduct(Product product,SODataDataContext db)
        {
            /*using (SODataDataContext db = DataContextFactory.CreateContext())
            {*/
                try
                {
                    ProductEntity entity = Mapper.ToProductEntity(product);

                    db.ProductEntities.Attach(entity, true);

                    //db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            //}
        }

        public int UpdatePlan(Product product, SODataDataContext db)
        {
            /*using (SODataDataContext db = DataContextFactory.CreateContext())
            {*/
                try
                {
                    QuoPlanEntity entity = Mapper.ToQuoPlanEntity(product);
                    db.QuoPlanEntities.Attach(entity, true);
                    //db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            //}
        }

        public int UpdateProductCategory(Product product)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    ProductCategoryEntity entity = Mapper.ToProductCategoryEntity(product);
                    db.ProductCategoryEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }



        public Product GetOneProduct(string productCode)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToProductObject(db.ProductEntities
                            .SingleOrDefault(p => p.PlanCode == productCode));
            }
        }

    }
}
